package com.qfhd.soft.cjzcore.Core;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CJTime {

    private static long DifferenceTime = 0;

    //比对当地和服务器的时间差，防止修改本地时间 影响正常任务的时间 的不匹配
    public static void SetServerTimeStamp(long serverTimeStamp){
        long localTimeStamp = getTimestamp();
        DifferenceTime = serverTimeStamp - localTimeStamp;
    }

    public static long getDifferenceTime(){
        return DifferenceTime;
    }

    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    public static long getTodayBeginTimeMillis() {
        Calendar calendar = Calendar.getInstance();
        long timestamp = System.currentTimeMillis();
        timestamp = timestamp - calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 - calendar.get(Calendar.MINUTE) * 60 * 1000 - calendar.get(Calendar.SECOND) * 1000 - calendar.get(Calendar.MILLISECOND);
        return timestamp;
    }

    public static long getTodayEndTimeMillis() {
        Calendar calendar = Calendar.getInstance();
        long timestamp = System.currentTimeMillis();
        timestamp = timestamp + 24 * 60 * 60 * 1000 - calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 - calendar.get(Calendar.MINUTE) * 60 * 1000 - calendar.get(Calendar.SECOND) * 1000 - calendar.get(Calendar.MILLISECOND);
        return timestamp - 1;
    }

    public static int getWeekday() {
        return getWeekday(System.currentTimeMillis());
    }

    public static int getWeekday(long timeMillis) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeMillis);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    public static long getTimestamp() {
        return System.currentTimeMillis() / 1000;
    }

    public static void SleepMillisecond(long millisecond) {
        try {
            Thread.sleep(millisecond);
        } catch (Exception e) {
        }
    }

    public static String getDateToString(long milSecond, String pattern) {
        Date date = new Date(milSecond);
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static void SleepSecond(long seconds) {
        try {
            Thread.sleep(1000 * seconds);
        } catch (Exception e) {
        }
    }

    public static void SleepMinute(long minutes) {
        try {
            Thread.sleep(1000 * 60 * minutes);
        } catch (Exception e) {
        }
    }

    public static void SleepHour(long hours) {
        try {
            Thread.sleep(1000 * 60 * 60 * hours);
        } catch (Exception e) {
        }
    }

    public static long getTodayBeginTimestamp() {
        Calendar calendar = Calendar.getInstance();
        long timestamp = System.currentTimeMillis();
        timestamp = timestamp - calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 - calendar.get(Calendar.MINUTE) * 60 * 1000 - calendar.get(Calendar.SECOND) * 1000;
        return timestamp / 1000;
    }

    public static long getTodayEndTimestamp() {
        Calendar calendar = Calendar.getInstance();
        long timestamp = System.currentTimeMillis();
        timestamp = timestamp + 24 * 60 * 60 * 1000 - calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 - calendar.get(Calendar.MINUTE) * 60 * 1000 - calendar.get(Calendar.SECOND) * 1000;
        return timestamp / 1000;
    }

    public static long getDayBeginTimestamp(long timestamp) {
        timestamp = timestamp * 1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        timestamp = timestamp - calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 - calendar.get(Calendar.MINUTE) * 60 * 1000 - calendar.get(Calendar.SECOND) * 1000;
        return timestamp / 1000;
    }

    public static long getDayEndTimestamp(long timestamp) {
        timestamp = timestamp * 1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timestamp);
        timestamp = timestamp + 24 * 60 * 60 * 1000 - calendar.get(Calendar.HOUR_OF_DAY) * 60 * 60 * 1000 - calendar.get(Calendar.MINUTE) * 60 * 1000 - calendar.get(Calendar.SECOND) * 1000;
        return timestamp / 1000;
    }

    //获取当月开始时间戳
    public static long getMonthStartTimestamp(long timestamp){
        try {
            String format = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat dft = new SimpleDateFormat(format);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(timestamp * 1000);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            String start_time = year + "-" + (month + 1) + "-1 0:00:00";
            Date start_dateTime = dft.parse(start_time);
            CJLogger.d("start_dateTime:" + start_dateTime.getTime() / 1000);
            return start_dateTime.getTime() / 1000;
        }catch (Exception e){

        }
        return 0;
    }

    //获取当月结束时间戳
    public static long getMonthEndTimestamp(long timestamp){
        try {
            String format = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat dft = new SimpleDateFormat(format);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(timestamp * 1000);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            String end_time = year + "-" + (month + 2) + "-1 0:00:00";
            Date end_dateTime = dft.parse(end_time);
            return (end_dateTime.getTime() / 1000) - 1;
        }catch (Exception e){

        }
        return 0;
    }

    public static long getDayStartTimestamp(long timestamp){
        try {
            String format = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat dft = new SimpleDateFormat(format);
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(timestamp * 1000);
            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int day     = calendar.get(Calendar.DAY_OF_MONTH);
            String end_time = year + "-" + (month + 1) + "-" + day + " 0:00:00";
            CJLogger.v("time :" + end_time);
            Date end_dateTime = dft.parse(end_time);
            return (end_dateTime.getTime() / 1000);
        }catch (Exception e){

        }
        return 0;
    }

    //获取当前时间格式 // 2019-11-6
    public  static String getLoackFormatYYHHDD(){
        long time=System.currentTimeMillis();
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static String getFormatYYMMDDHHDDmm(Long timeStamp){
        long time=timeStamp;
        CJLogger.v("getFormatHHDDmm :" + timeStamp);
        Date date = new Date(time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String getTimeFormatYYMMDD(Long timeStamp){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(timeStamp);
        return format.format(date);
    }

    public static String getTimeFormatHHMM(Long timeStamp){
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date date = new Date(timeStamp);
        return format.format(date);
    }

    public static String getTimeStringFromTimeStamp(Long timeStamp){
//        Long time = timeStamp - getCurrentTimeMillis()/1000;
//        CJLogger.v("4_timeStamp:" + time + " time:" + timeStamp + " current:" + getCurrentTimeMillis()/1000 + "diffentTiem:" + CJTime.getDifferenceTime());
//        long hour = time / 3600 / 60;
//        long min = (time / 60) % 60;
//        long second = time % 60;
//        return DecimalFormatHelper.getInt_2((int)hour) + ":" + DecimalFormatHelper.getInt_2((int)min) + ":" + DecimalFormatHelper.getInt_2((int)second);
        return getFormatYYMMDDHHDDmm(timeStamp*1000);
    }

    public static String getTimeFormatMMDD(Long timeStamp){
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
        Date date = new Date(timeStamp);
        return format.format(date);
    }
}
